package com.example.canopytemperature;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.canopytemperature.Bean.NodeBean;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.canopytemperature.Utils;

public class MainActivity extends AppCompatActivity {
    final ArrayList airHumList = new ArrayList();
    final ArrayList airTemList = new ArrayList();
    Button cancel;
    boolean check = true;
    Context context;
    int diai;
    Dialog dialog;
    RadioGroup dialog_rg;

    /* renamed from: id */
    ArrayList f73id = new ArrayList();
    final ArrayList illuminationList = new ArrayList();
    EditText inputTime;
    long limitTime = 60000;
    ListView nodeList;
    LinearLayout nullImg;
    RadioButton radioButton1;
    final ArrayList soilHumList = new ArrayList();
    final ArrayList soilTemList = new ArrayList();
    Button sumbit;
    ImageView timeDialog;
    final ArrayList timeList = new ArrayList();
    ArrayList titleList = new ArrayList();

    /* access modifiers changed from: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(getColor(R.color.colorPrimary));
        setContentView((int) R.layout.activity_main);
        initViews();
        initActions();

    }

    /* access modifiers changed from: protected */
    public void onPause() {
        super.onPause();
        this.check = false;

    }

    /* access modifiers changed from: protected */
    public void onResume() {
        super.onResume();
        diai = Utils.getInt(getApplicationContext(),"1","index");
        limitTime = Utils.getLong(getApplicationContext(),"1","time");
        this.check = true;
        setDatas();
    }

    // 用来计算返回键的点击间隔时间
     private long exitTime = 0;
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                        //弹出提示，可以有多种方式
                        Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_LONG).show();
                        exitTime = System.currentTimeMillis();
                    } else {
                        finish();
                    }
                return true;
            }
                return super.onKeyDown(keyCode, event);
     }

    private void setDatas() {
        new Thread(new Runnable() {
            public void run() {
                while (MainActivity.this.check) {
                    MainActivity.this.initDatas();
                    try {
                        Thread.sleep(MainActivity.this.limitTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void initActions() {
        this.dialog = new Dialog(this.context);
        this.dialog.setContentView(R.layout.time_dialog);
        this.dialog_rg = (RadioGroup) this.dialog.findViewById(R.id.dialog_rg);
        this.radioButton1 = (RadioButton) this.dialog.findViewById(R.id.radio1);
        this.radioButton1.setChecked(true);
        this.cancel = (Button) this.dialog.findViewById(R.id.cancel);
        this.sumbit = (Button) this.dialog.findViewById(R.id.sumbit);
        this.inputTime = (EditText) this.dialog.findViewById(R.id.inputTime);
        this.nodeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this.context, NodeListDetail.class);
                intent.putExtra("equ_id", MainActivity.this.f73id.get(i).toString());
                intent.putExtra("title", MainActivity.this.titleList.get(i).toString());
                intent.putExtra("time", MainActivity.this.timeList.get(i).toString());
                MainActivity.this.startActivity(intent);
            }
        });
        this.timeDialog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                MainActivity.this.dialog.show();
            }
        });
        this.dialog_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                MainActivity mainActivity = MainActivity.this;
                mainActivity.radioButton1 = (RadioButton) mainActivity.dialog_rg.findViewById(i);
                int index = radioGroup.indexOfChild(MainActivity.this.radioButton1);
                if (index == 3) {
                    MainActivity.this.inputTime.setEnabled(true);
                } else {
                    MainActivity.this.inputTime.setEnabled(false);
                }
                MainActivity.this.diai = index;
            }
        });
        this.cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                MainActivity.this.dialog.dismiss();
            }
        });
        this.sumbit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String inputTimeText = MainActivity.this.inputTime.getText().toString().trim();
                int i = MainActivity.this.diai;
                if (i == 0) {
                    MainActivity.this.limitTime = 60000;
                } else if (i == 1) {
                    MainActivity.this.limitTime = 300000;
                } else if (i == 2) {
                    MainActivity.this.limitTime = 3600000;
                } else if (i == 3) {
                    if (TextUtils.isEmpty(inputTimeText)) {
                        Toast.makeText(MainActivity.this.context, "自定义不能为空！", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (Integer.parseInt(inputTimeText) <= 0 || Integer.parseInt(inputTimeText) >= 60) {
                        Toast.makeText(MainActivity.this.context, "自定义值范围为0~60！", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        MainActivity mainActivity = MainActivity.this;
                        mainActivity.limitTime = (long) (Integer.parseInt(mainActivity.inputTime.getText().toString()) * 1000);
                    }
                }
                Utils.putInt(getApplicationContext(),"1","index",i);
                Utils.putLong(getApplicationContext(),"1","time",limitTime);
                MainActivity mainActivity2 = MainActivity.this;
                mainActivity2.check = true;
                mainActivity2.dialog.dismiss();
            }
        });
    }

    /* access modifiers changed from: private */
    public void initDatas() {
        OkHttpHelper.Get("/Greenhouse/getListAll", new Callback() {
            public void onFailure(Call call, IOException e) {
                Log.i("err1111111111", "onFailure: " + e.getMessage());
            }

            public void onResponse(Call call, Response response) throws IOException {
                MainActivity.this.f73id.clear();
                MainActivity.this.titleList.clear();
                MainActivity.this.airTemList.clear();
                MainActivity.this.soilTemList.clear();
                MainActivity.this.illuminationList.clear();
                MainActivity.this.airHumList.clear();
                MainActivity.this.soilHumList.clear();
                MainActivity.this.timeList.clear();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response.body().string());
                    if (jsonObject.getInt("code") == 400) {
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                MainActivity.this.nullImg.setVisibility(View.VISIBLE);
                                MainActivity.this.nodeList.setVisibility(View.GONE);
                            }
                        });
                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        MainActivity.this.nullImg.setVisibility(View.GONE);
                        MainActivity.this.nodeList.setVisibility(View.VISIBLE);
                    }
                });
                Gson gson = new Gson();
                final NodeBean nodeBean = (NodeBean) gson.fromJson(jsonObject + "", NodeBean.class);
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        if (nodeBean.getCode() == 200) {
                            for (int i = 0; i < nodeBean.getData().size(); i++) {
                                String airHumListSwich = nodeBean.getData().get(i).getAirnum();
                                String airTemListSwich = nodeBean.getData().get(i).getAirtem();
                                String soilTemListSwich = nodeBean.getData().get(i).getSoiltem();
                                String IlluminationListSwich = nodeBean.getData().get(i).getIllumination();
                                String soilHumListSwich = nodeBean.getData().get(i).getSoilhum();
                                MainActivity.this.f73id.add(nodeBean.getData().get(i).getEqu_id());
                                MainActivity.this.titleList.add(nodeBean.getData().get(i).getTitle());
                                MainActivity.this.airTemList.add(airTemListSwich);
                                MainActivity.this.soilTemList.add(soilTemListSwich);
                                MainActivity.this.illuminationList.add(IlluminationListSwich);
                                MainActivity.this.airHumList.add(airHumListSwich);
                                MainActivity.this.soilHumList.add(soilHumListSwich);
                                MainActivity.this.timeList.add(nodeBean.getData().get(i).getCreate_time());
                            }
                            NodeListAdapter nodeListAdapter = new NodeListAdapter(MainActivity.this.getApplicationContext(), MainActivity.this.airTemList, MainActivity.this.soilTemList, MainActivity.this.illuminationList, MainActivity.this.airHumList, MainActivity.this.soilHumList, MainActivity.this.titleList, MainActivity.this.f73id);
                            MainActivity.this.nodeList.setAdapter(nodeListAdapter);
                            nodeListAdapter.notifyDataSetChanged();
                            return;
                        }
                        Toast.makeText(MainActivity.this.context, nodeBean.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void initViews() {
        this.context = this;
        this.nodeList = (ListView) findViewById(R.id.node_list);
        this.timeDialog = (ImageView) findViewById(R.id.node_time);
        this.nullImg = (LinearLayout) findViewById(R.id.null_img);
    }
}
